home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / filecmp.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  10KB  |  314 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Utilities for comparing files and directories.
  5.  
  6. Classes:
  7.     dircmp
  8.  
  9. Functions:
  10.     cmp(f1, f2, shallow=1) -> int
  11.     cmpfiles(a, b, common) -> ([], [], [])
  12.  
  13. '''
  14. import os
  15. import stat
  16. from itertools import ifilter, ifilterfalse, imap, izip
  17. __all__ = [
  18.     'cmp',
  19.     'dircmp',
  20.     'cmpfiles']
  21. _cache = { }
  22. BUFSIZE = 8192
  23.  
  24. def cmp(f1, f2, shallow = 1):
  25.     '''Compare two files.
  26.  
  27.     Arguments:
  28.  
  29.     f1 -- First file name
  30.  
  31.     f2 -- Second file name
  32.  
  33.     shallow -- Just check stat signature (do not read the files).
  34.                defaults to 1.
  35.  
  36.     Return value:
  37.  
  38.     True if the files are the same, False otherwise.
  39.  
  40.     This function uses a cache for past comparisons and the results,
  41.     with a cache invalidation mechanism relying on stale signatures.
  42.  
  43.     '''
  44.     s1 = _sig(os.stat(f1))
  45.     s2 = _sig(os.stat(f2))
  46.     if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
  47.         return False
  48.     if shallow and s1 == s2:
  49.         return True
  50.     if s1[1] != s2[1]:
  51.         return False
  52.     result = _cache.get((f1, f2))
  53.     if result and (s1, s2) == result[:2]:
  54.         return result[2]
  55.     outcome = _do_cmp(f1, f2)
  56.     _cache[(f1, f2)] = (s1, s2, outcome)
  57.     return outcome
  58.  
  59.  
  60. def _sig(st):
  61.     return (stat.S_IFMT(st.st_mode), st.st_size, st.st_mtime)
  62.  
  63.  
  64. def _do_cmp(f1, f2):
  65.     bufsize = BUFSIZE
  66.     fp1 = open(f1, 'rb')
  67.     fp2 = open(f2, 'rb')
  68.     while True:
  69.         b1 = fp1.read(bufsize)
  70.         b2 = fp2.read(bufsize)
  71.         if b1 != b2:
  72.             return False
  73.         if not b1:
  74.             return True
  75.         continue
  76.         b1
  77.  
  78.  
  79. class dircmp:
  80.     """A class that manages the comparison of 2 directories.
  81.  
  82.     dircmp(a,b,ignore=None,hide=None)
  83.       A and B are directories.
  84.       IGNORE is a list of names to ignore,
  85.         defaults to ['RCS', 'CVS', 'tags'].
  86.       HIDE is a list of names to hide,
  87.         defaults to [os.curdir, os.pardir].
  88.  
  89.     High level usage:
  90.       x = dircmp(dir1, dir2)
  91.       x.report() -> prints a report on the differences between dir1 and dir2
  92.        or
  93.       x.report_partial_closure() -> prints report on differences between dir1
  94.             and dir2, and reports on common immediate subdirectories.
  95.       x.report_full_closure() -> like report_partial_closure,
  96.             but fully recursive.
  97.  
  98.     Attributes:
  99.      left_list, right_list: The files in dir1 and dir2,
  100.         filtered by hide and ignore.
  101.      common: a list of names in both dir1 and dir2.
  102.      left_only, right_only: names only in dir1, dir2.
  103.      common_dirs: subdirectories in both dir1 and dir2.
  104.      common_files: files in both dir1 and dir2.
  105.      common_funny: names in both dir1 and dir2 where the type differs between
  106.         dir1 and dir2, or the name is not stat-able.
  107.      same_files: list of identical files.
  108.      diff_files: list of filenames which differ.
  109.      funny_files: list of files which could not be compared.
  110.      subdirs: a dictionary of dircmp objects, keyed by names in common_dirs.
  111.      """
  112.     
  113.     def __init__(self, a, b, ignore = None, hide = None):
  114.         self.left = a
  115.         self.right = b
  116.         if hide is None:
  117.             self.hide = [
  118.                 os.curdir,
  119.                 os.pardir]
  120.         else:
  121.             self.hide = hide
  122.         if ignore is None:
  123.             self.ignore = [
  124.                 'RCS',
  125.                 'CVS',
  126.                 'tags']
  127.         else:
  128.             self.ignore = ignore
  129.  
  130.     
  131.     def phase0(self):
  132.         self.left_list = _filter(os.listdir(self.left), self.hide + self.ignore)
  133.         self.right_list = _filter(os.listdir(self.right), self.hide + self.ignore)
  134.         self.left_list.sort()
  135.         self.right_list.sort()
  136.  
  137.     
  138.     def phase1(self):
  139.         a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
  140.         b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
  141.         self.common = map(a.__getitem__, ifilter(b.__contains__, a))
  142.         self.left_only = map(a.__getitem__, ifilterfalse(b.__contains__, a))
  143.         self.right_only = map(b.__getitem__, ifilterfalse(a.__contains__, b))
  144.  
  145.     
  146.     def phase2(self):
  147.         self.common_dirs = []
  148.         self.common_files = []
  149.         self.common_funny = []
  150.         for x in self.common:
  151.             a_path = os.path.join(self.left, x)
  152.             b_path = os.path.join(self.right, x)
  153.             ok = 1
  154.             
  155.             try:
  156.                 a_stat = os.stat(a_path)
  157.             except os.error:
  158.                 why = None
  159.                 ok = 0
  160.  
  161.             
  162.             try:
  163.                 b_stat = os.stat(b_path)
  164.             except os.error:
  165.                 why = None
  166.                 ok = 0
  167.  
  168.             if ok:
  169.                 a_type = stat.S_IFMT(a_stat.st_mode)
  170.                 b_type = stat.S_IFMT(b_stat.st_mode)
  171.                 if a_type != b_type:
  172.                     self.common_funny.append(x)
  173.                 elif stat.S_ISDIR(a_type):
  174.                     self.common_dirs.append(x)
  175.                 elif stat.S_ISREG(a_type):
  176.                     self.common_files.append(x)
  177.                 else:
  178.                     self.common_funny.append(x)
  179.             a_type != b_type
  180.             self.common_funny.append(x)
  181.         
  182.  
  183.     
  184.     def phase3(self):
  185.         xx = cmpfiles(self.left, self.right, self.common_files)
  186.         (self.same_files, self.diff_files, self.funny_files) = xx
  187.  
  188.     
  189.     def phase4(self):
  190.         self.subdirs = { }
  191.         for x in self.common_dirs:
  192.             a_x = os.path.join(self.left, x)
  193.             b_x = os.path.join(self.right, x)
  194.             self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)
  195.         
  196.  
  197.     
  198.     def phase4_closure(self):
  199.         self.phase4()
  200.         for sd in self.subdirs.itervalues():
  201.             sd.phase4_closure()
  202.         
  203.  
  204.     
  205.     def report(self):
  206.         print 'diff', self.left, self.right
  207.         if self.left_only:
  208.             self.left_only.sort()
  209.             print 'Only in', self.left, ':', self.left_only
  210.         
  211.         if self.right_only:
  212.             self.right_only.sort()
  213.             print 'Only in', self.right, ':', self.right_only
  214.         
  215.         if self.same_files:
  216.             self.same_files.sort()
  217.             print 'Identical files :', self.same_files
  218.         
  219.         if self.diff_files:
  220.             self.diff_files.sort()
  221.             print 'Differing files :', self.diff_files
  222.         
  223.         if self.funny_files:
  224.             self.funny_files.sort()
  225.             print 'Trouble with common files :', self.funny_files
  226.         
  227.         if self.common_dirs:
  228.             self.common_dirs.sort()
  229.             print 'Common subdirectories :', self.common_dirs
  230.         
  231.         if self.common_funny:
  232.             self.common_funny.sort()
  233.             print 'Common funny cases :', self.common_funny
  234.         
  235.  
  236.     
  237.     def report_partial_closure(self):
  238.         self.report()
  239.         for sd in self.subdirs.itervalues():
  240.             print 
  241.             sd.report()
  242.         
  243.  
  244.     
  245.     def report_full_closure(self):
  246.         self.report()
  247.         for sd in self.subdirs.itervalues():
  248.             print 
  249.             sd.report_full_closure()
  250.         
  251.  
  252.     methodmap = dict(subdirs = phase4, same_files = phase3, diff_files = phase3, funny_files = phase3, common_dirs = phase2, common_files = phase2, common_funny = phase2, common = phase1, left_only = phase1, right_only = phase1, left_list = phase0, right_list = phase0)
  253.     
  254.     def __getattr__(self, attr):
  255.         if attr not in self.methodmap:
  256.             raise AttributeError, attr
  257.         attr not in self.methodmap
  258.         self.methodmap[attr](self)
  259.         return getattr(self, attr)
  260.  
  261.  
  262.  
  263. def cmpfiles(a, b, common, shallow = 1):
  264.     """Compare common files in two directories.
  265.  
  266.     a, b -- directory names
  267.     common -- list of file names found in both directories
  268.     shallow -- if true, do comparison based solely on stat() information
  269.  
  270.     Returns a tuple of three lists:
  271.       files that compare equal
  272.       files that are different
  273.       filenames that aren't regular files.
  274.  
  275.     """
  276.     res = ([], [], [])
  277.     for x in common:
  278.         ax = os.path.join(a, x)
  279.         bx = os.path.join(b, x)
  280.         res[_cmp(ax, bx, shallow)].append(x)
  281.     
  282.     return res
  283.  
  284.  
  285. def _cmp(a, b, sh, abs = abs, cmp = cmp):
  286.     
  287.     try:
  288.         return not abs(cmp(a, b, sh))
  289.     except os.error:
  290.         return 2
  291.  
  292.  
  293.  
  294. def _filter(flist, skip):
  295.     return list(ifilterfalse(skip.__contains__, flist))
  296.  
  297.  
  298. def demo():
  299.     import sys as sys
  300.     import getopt as getopt
  301.     (options, args) = getopt.getopt(sys.argv[1:], 'r')
  302.     if len(args) != 2:
  303.         raise getopt.GetoptError('need exactly two args', None)
  304.     len(args) != 2
  305.     dd = dircmp(args[0], args[1])
  306.     if ('-r', '') in options:
  307.         dd.report_full_closure()
  308.     else:
  309.         dd.report()
  310.  
  311. if __name__ == '__main__':
  312.     demo()
  313.  
  314.